#!/usr/bin/env python
# coding=utf-8
from django import newforms as forms
from google.appengine.ext.db import djangoforms
import models

class AccountForm(djangoforms.ModelForm):
    "This model form for account"
    class Meta:
        model = models.Account
        exclude = ['user']

class AccountCustomForm(forms.Form):
    "This customized form for account"
    # exclude user, createdDatetime
    name = forms.CharField(label="Account name", required=True, max_length=256)
    # GAE's python is too old to use FloatField, I need clean it after send
    initialValue = forms.CharField(label="Initial Value", required=True)
    accountType = forms.ChoiceField(label="Account Type", required=True, choices=[(1, 'Cash'), (2, 'Bank account'), (3, 'Credit card'), (4, 'Loan')])
    note= forms.CharField(label='Note')

class CategoryForm(djangoforms.ModelForm):
    class Meta:
        model = models.Category
        exclude = ['user']

class CategoryCustomForm(forms.Form):
    "This customized form for category"
    # exclude user, createdDatetime
    name = forms.CharField(label="Category name", required=True, max_length=256)
    order = forms.IntegerField(label="Category order", required=True)
    note= forms.CharField(label='Note')
        
class RecordForm(djangoforms.ModelForm):
    class Meta:
        model = models.Record

class RecordCustomForm(forms.Form):
    "This customized form for record"
    # exclude createdDatetime
    account_key = forms.ChoiceField(label="Account", required=True)
    action = forms.ChoiceField(label="Action", required=True, choices=[(1, 'Earn'), (2, 'Cost'), (3, 'Transfer')])
    value = forms.CharField(label="Value", required=True)
    recordDate = forms.DateField(label='Date', required=True)
    reason = forms.CharField(label="Reason", required=True, max_length=256)
    category_key = forms.ChoiceField(label="Category", required=False)
    note= forms.CharField(label='Note')
    
    def __init__(self, user, *args, **kwargs):
        self.user = user
        super(RecordCustomForm, self).__init__(*args, **kwargs) 
        self.fields['account_key'].choices = []
        self.fields['category_key'].choices = [('', '其他')]
        accounts = models.Account.all().filter('user = ', user)
        categories = models.Category.all().filter('user = ', user).order('order')
        for a in accounts:
            self.fields['account_key'].choices.append((a.key(), a.name))
        for c in categories:
            self.fields['category_key'].choices.append((c.key(), c.name))
         

class TransactionCustomForm(forms.Form):
    "This customized form for transaction"
    source_account_key = forms.ChoiceField(label="Source Account", required=True)
    destination_account_key = forms.ChoiceField(label="Destination Account", required=True)
    value = forms.CharField(label="Value", required=True)
    recordDate = forms.DateField(label='Date', required=True)
    reason = forms.CharField(label="Reason", required=True, max_length=256)
    category_key = forms.ChoiceField(label="Category", required=False)
    note= forms.CharField(label='Note')

    def __init__(self, user, *args, **kwargs):
        self.user = user
        super(TransactionCustomForm, self).__init__(*args, **kwargs) 
        self.fields['source_account_key'].choices = []
        self.fields['destination_account_key'].choices = []
        self.fields['category_key'].choices = [('', '其他')]
        accounts = models.Account.all().filter('user = ', user)
        categories = models.Category.all().filter('user = ', user).order('order')
        for a in accounts:
            self.fields['source_account_key'].choices.append((a.key(), a.name))
            self.fields['destination_account_key'].choices.append((a.key(), a.name))
        for c in categories:
            self.fields['category_key'].choices.append((c.key(), c.name))    